﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserControlLib
{
    /// <summary>
    /// MulNumberScrollingControl.xaml 的交互逻辑
    /// </summary>
    public partial class MulNumberScrollingControl : UserControl
    {
        private ObservableCollection<NumberScrollingControl> _items;
        private bool _firstLoaded = true; // 添加这个字段
        public MulNumberScrollingControl()
        {
            InitializeComponent();
            this.DataContext = this;
            _items = new ObservableCollection<NumberScrollingControl>();
            UpdateItems();
        }
      
        public static readonly DependencyProperty ItemHeightProperty =
            DependencyProperty.Register("ItemHeight", typeof(double), typeof(MulNumberScrollingControl),
                new PropertyMetadata(30.0, OnItemHeightChanged));

        public static readonly DependencyProperty ItemWidthProperty =
            DependencyProperty.Register("ItemWidth", typeof(double), typeof(MulNumberScrollingControl),
                new PropertyMetadata(40.0, OnItemWidthChanged));

        public static readonly DependencyProperty ItemCountProperty =
            DependencyProperty.Register("ItemCount", typeof(int), typeof(MulNumberScrollingControl),
                new PropertyMetadata(1, OnItemCountChanged));

        public static readonly DependencyProperty NumberProperty =
    DependencyProperty.Register("Number", typeof(int), typeof(MulNumberScrollingControl),
        new PropertyMetadata(0, OnNumberChanged));

        public static readonly DependencyProperty BackgroundProperty =
    DependencyProperty.Register("Background", typeof(Brush), typeof(MulNumberScrollingControl),
        new PropertyMetadata(null, OnBackgroundChanged));

        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }

        private static void OnBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MulNumberScrollingControl;
            if (control != null)
            {
                control.UpdateItems();
            }
        }

        public static readonly DependencyProperty ForegroundProperty =
            DependencyProperty.Register("Foreground", typeof(Brush), typeof(MulNumberScrollingControl),
                new PropertyMetadata(null, OnForegroundChanged));

        public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }

        private static void OnForegroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MulNumberScrollingControl;
            if (control != null)
            {
                control.UpdateItems();
            }
        }

        public int Number
        {
            get { return (int)GetValue(NumberProperty); }
            set { SetValue(NumberProperty, value); }
        }

        private static void OnNumberChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MulNumberScrollingControl;
            if (control != null)
            {
                control.UpdateItems();
            }
        }


        public double ItemHeight
        {
            get { return (double)GetValue(ItemHeightProperty); }
            set { SetValue(ItemHeightProperty, value); }
        }

        public double ItemWidth
        {
            get { return (double)GetValue(ItemWidthProperty); }
            set { SetValue(ItemWidthProperty, value); }
        }

        public int ItemCount
        {
            get { return (int)GetValue(ItemCountProperty); }
            set { SetValue(ItemCountProperty, value); }
        }

        public ObservableCollection<NumberScrollingControl> Items
        {
            get { return _items; }
            private set
            {
                if (_items != null)
                {
                    _items.CollectionChanged -= Items_CollectionChanged;
                }

                _items = value;

                if (_items != null)
                {
                    _items.CollectionChanged += Items_CollectionChanged;
                }

                OnPropertyChanged(nameof(Items));
            }
        }

        private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            CollectionChanged?.Invoke(this, e);
        }

        private static void OnItemHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MulNumberScrollingControl;
            if (control != null)
            {
                control.UpdateItems();
            }
        }

        private static void OnItemWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MulNumberScrollingControl;
            if (control != null)
            {
                control.UpdateItems();
            }
        }

        private static void OnItemCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MulNumberScrollingControl;
            if (control != null)
            {
                control.UpdateItems();
            }
        }

        private void UpdateItems()
        {
            // 将 Number 转换为字符串，并获取其长度
            string numberString = Number.ToString();
            int targetCount = numberString.Length;

            // 更新 ItemCount，确保它与数字的长度匹配
            ItemCount = targetCount;


            _items.Clear();
            for (int i = 0; i < ItemCount; i++)
            {
                char digitChar = numberString[i];
                int digit = digitChar - '0'; // 将字符转换为整数

                var item = new NumberScrollingControl
                {
                    Height = ItemHeight,
                    Width = ItemWidth,
                    Background=Background,
                    Foreground=Foreground,
                    Num = digit
                };
                _items.Add(item);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (_firstLoaded)
            {
                _firstLoaded = false;
                UpdateItems();
            }
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
